// 拷贝继承：把原型对象是属性拷贝一份给另一个原型对象
// 对象属性的浅拷贝
function clone(parent) {
  let child = {}
  // 获得parent对象所有可遍历的属性名称数组
  let keys_parent = Object.keys(parent) // ['id','name','dog']
  for (let key of keys_parent) {
    // child添加属性key并获得值parent[key]
    child[key] = parent[key] // parent['dog'] = { name: '旺财' }
  }
  return child
}

function isObject(n) {
  return Object.prototype.toString.call(n).slice(8, -1) === 'Object'
}
function isArray(n) {
  return Object.prototype.toString.call(n).slice(8, -1) === 'Array'
}

// 对象属性的深拷贝
function cloneDeep(obj) {
  if (!isObject(obj) && !isArray(obj)) {
    return console.log('请传递对象参数')
  }
  let object = isArray(obj) ? [] : {}
  // 获得对象的所有属性名称数组
  let keys = Reflect.ownKeys(obj)
  for (let key of keys) {
    object[key] = isObject(obj[key]) || isArray(obj[key]) ? cloneDeep(obj[key]) : obj[key]
  }
  return object
}

let parent = { id: 1, name: 'tom', dog: { name: '旺财' }, list: [1, 2, 3] }
let child = cloneDeep(parent)
parent.list[1] = '100'
console.log('parent =', parent)
console.log('child =', child)

function Person(gender) {
  this.favorColor = ['red', 'blue']
  this.gender = gender
}
Person.prototype.head = 1
Person.prototype.foot = 2
Person.prototype.show = function () {
  console.log(`{
    name:${this.name},
    no:${this.no},
    foot:${this.foot},
    head:${this.head},
    favorColor:${this.favorColor},
    gender:${this.gender},
  }`)
}
function Student(name, no, gender) {
  Person.apply(this, [gender])
  this.name = name
  this.no = no
}
Student.prototype = cloneDeep(Person.prototype)
let stu = new Student('tom', 1, '女')
// console.log(stu)
// stu.show()

/*
  json拷贝方法:
    1. let b = JSON.parse(JSON.stringify(o))
    2. 是深度拷贝，但是不能拷贝对象的函数属性
    3. 不能拷贝非枚举的属性([Symbol()])
*/
let o = {
  id: 1,
  name: 'tom',
  obj: { id: 2, name: 'sam' },
  arr: [1, 2, 3],
  [Symbol()]: '123',
}
let str = JSON.stringify(o)
// console.log(str)
let b = JSON.parse(str)
// console.log(b === o)
o.obj.name = 'abc'
// console.log(b)

// =、==、=== 的区别
// a instanceof Object a是否是object类型
let rt = stu instanceof Object // true
let rt1 = stu instanceof Student // true
// console.log(rt, rt1)
